<?php

namespace app\admin\model;

use think\Model;


class Order extends Model
{

    

    

    // 表名
    protected $name = 'order';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'status_text',
        'use_type_text',
        'car_type_text',
        'paytime_text',
        'taking_time_text',
        'completion_time_text'
    ];
    

    
    public function getStatusList()
    {
        return ['1' => __('Status 1'), '2' => __('Status 2'), '3' => __('Status 3'), '4' => __('Status 4'), '5' => __('Status 5'), '6' => __('检测中')];
    }

    public function getUseTypeList()
    {
        return ['1' => __('Use_type 1'), '2' => __('Use_type 2')];
    }

    public function getCarTypeList()
    {
        return ['1' => __('Car_type 1'), '2' => __('Car_type 2')];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getUseTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['use_type']) ? $data['use_type'] : '');
        $list = $this->getUseTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getCarTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['car_type']) ? $data['car_type'] : '');
        $list = $this->getCarTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getPaytimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['paytime']) ? $data['paytime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getTakingTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['taking_time']) ? $data['taking_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getCompletionTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['completion_time']) ? $data['completion_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setPaytimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setTakingTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setCompletionTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function user()
    {
        return $this->belongsTo('User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function detection()
    {
        return $this->belongsTo('User', 'detection_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
